import { Action } from '../casl/action.enum'
import { PolicyHandlerCallback, SimplePolicyHandler } from '../interfaces/policy-handler.interface'
import { CheckPolicies } from './auth.decorator'

// should only be used when we are checking if user can modify some specific kind of model
// not a specie of said model
// this one will be a great fit for:
// builder.can(Action, Model)
// but not
// builder.can(Action, Model, { ...additionalRequirements })
export const SimpleModelPolicy = 
  (action: Action, subject: any, handler: PolicyHandlerCallback = () => true) => 
    CheckPolicies(
      SimplePolicyHandler(
        (ab, ctx, token) => handler(ab, ctx, token) && ab.can(action, subject),
      ),
    )